package com.ts.Controller;

import com.ts.model.Bkryyj;
import com.ts.model.BusRun;
import com.ts.model.Camera;
import com.ts.service.BigdataService;
import com.ts.service.TransitService;
import com.ts.utils.DataTableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import  com.ts.utils.ResultSet;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/bigdata")
public class BkryyjController {

    @Autowired
    private BigdataService bigdataService;
    @Autowired
    private TransitService transitService;

    /**
     * 视频预警_布控人员预警列表查询接口
     *
     * @param bjdd     报警地点
     * @param keywords 关键词（支持姓名、证件号码模糊查询）
     */
    @RequestMapping("/bkryyjList")
    @ResponseBody
    public DataTableVo<Bkryyj> getBkryyjList(@RequestParam(name = "bjdd", required = false) String bjdd,
                                             @RequestParam(name = "keyWords", required = false) String keywords,
                                             HttpServletResponse response) {

        response.addHeader("Access-Control-Allow-Orrigin", "*");
        DataTableVo<Bkryyj> resultModel = new DataTableVo<Bkryyj>();

        try {
            Map<String, String> params = new HashMap<String, String>();
            //查询近两小时的预警数据
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            String starttime = sdf.format(new Date());
            params.put("startTime", starttime);
            params.put("bjdd", bjdd);
            params.put("keywords", keywords);
            List<Bkryyj> bkryyjList = bigdataService.getBkryyjList(params);

            //换PGIS之后删掉
            /*for (Bkryyj bkryyj:
                 bkryyjList) {
                if(!StringUtil.isBlank(bkryyj.getWd()) && !StringUtil.isBlank(bkryyj.getJd())){
                    double[] bd = GpsToBaidu.wgs2bd(Double.parseDouble(bkryyj.getWd()), Double.parseDouble(bkryyj.getJd()));
                    bkryyj.setWd(bd[0]+"");
                    bkryyj.setJd(bd[1]+"");
                }
            }*/

            resultModel.setTotalCount(null != bkryyjList ? bkryyjList.size() : 0);
            resultModel.setData(bkryyjList);
            resultModel.setSuccess(true);
            resultModel.setMessage("查询成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultModel.setSuccess(false);
            resultModel.setMessage("查询失败！");
        } finally {
            return resultModel;
        }
    }

    /**
     * 车辆实时信息列表接口
     */
    @RequestMapping("/busrunList")
    @ResponseBody
    public DataTableVo<BusRun> getBusRunList(HttpServletResponse response,
                                             @RequestParam(name = "keywords", required = false) String keywords) {
        response.addHeader("Access-Control-Allow-Orrigin", "*");
        DataTableVo<BusRun> resultModel = new DataTableVo<BusRun>();

        try {
            Map<String, Object> params = new HashMap<String, Object>();
            String startTime = new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(new Date());
            params.put("keywords", keywords);
            params.put("startTime", startTime);

            List<BusRun> busrunList = transitService.getBusRunList(params);

            //换PGIS之后删掉
            /*for (BusRun busRun:
                    busrunList) {
                if(!StringUtil.isBlank(busRun.getLat()) && !StringUtil.isBlank(busRun.getLng())){
                    double[] bd = GpsToBaidu.wgs2bd(Double.parseDouble(busRun.getLat()), Double.parseDouble(busRun.getLng()));
                    busRun.setLat(bd[0]+"");
                    busRun.setLng(bd[1]+"");
                }
            }*/

            resultModel.setTotalCount(null != busrunList ? busrunList.size() : 0);
            resultModel.setData(busrunList);
            resultModel.setSuccess(true);
            resultModel.setMessage("查询成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultModel.setSuccess(false);
            resultModel.setMessage("查询失败！");
        } finally {
            return resultModel;
        }
    }

    /**
     * 获取摄像头列表接口
     *
     * @param name 相机名称
     */
    @RequestMapping("/cameraList")
    @ResponseBody
    public DataTableVo<Camera> getCameraList(@RequestParam(name = "name", required = false) String name, HttpServletResponse response) {

        response.addHeader("Access-Control-Allow-Orrigin", "*");
        DataTableVo<Camera> resultModel = new DataTableVo<Camera>();

        try {
            List<Camera> cameraList = transitService.getCameraList(name);

            //换PGIS之后删掉c
            /*for (Camera camera:
                    cameraList) {
                if(!StringUtil.isBlank(camera.getLatitude()) && !StringUtil.isBlank(camera.getLongitude())){
                    double[] bd = GpsToBaidu.wgs2bd(Double.parseDouble(camera.getLatitude()), Double.parseDouble(camera.getLongitude()));
                    camera.setLatitude(bd[0]+"");
                    camera.setLongitude(bd[1]+"");
                }
            }*/

            resultModel.setTotalCount(null != cameraList ? cameraList.size() : 0);
            resultModel.setData(cameraList);
            resultModel.setSuccess(true);
            resultModel.setMessage("查询成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultModel.setSuccess(false);
            resultModel.setMessage("查询失败！");
        } finally {
            return resultModel;
        }
    }

    /**
     * 常控预警地铁列表接口
     */
    @RequestMapping("/getCkyjDtList")
    @ResponseBody
    public DataTableVo<Map<String, Object>> getCkyjDtList(HttpServletResponse response) {

        response.addHeader("Access-Control-Allow-Orrigin", "*");
        DataTableVo<Map<String, Object>> resultModel = new DataTableVo<Map<String, Object>>();

        try {
            List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
            Map<String, Object> params = new HashMap<String, Object>();
            //预警总数
            params.put("lx", "地铁");
            Integer zdryTotalCount = bigdataService.getZdryyjCount(params);

            //今日预警数
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            params.put("startTime", sdf.format(new Date()));
            Integer zdryTodayCount = bigdataService.getZdryyjCount(params);

            //1大客流预警
            Map<String, Object> dklMap = new HashMap<String, Object>();
            dklMap.put("id", 0);
            dklMap.put("type", "大客流预警");
            dklMap.put("today", 0);
            dklMap.put("total", 0);

            //2重点人预警
            Map<String, Object> zdrMap = new HashMap<String, Object>();
            zdrMap.put("id", 1);
            zdrMap.put("type", "重点人预警");
            zdrMap.put("today", zdryTodayCount);
            zdrMap.put("total", zdryTotalCount);

            //3异常票卡预警
            Map<String, Object> ycpkMap = new HashMap<String, Object>();
            ycpkMap.put("id", 2);
            ycpkMap.put("type", "异常票卡预警");
            ycpkMap.put("today", 0);
            ycpkMap.put("total", 0);

            //4车扒预警
            Map<String, Object> cbMap = new HashMap<String, Object>();
            cbMap.put("id", 3);
            cbMap.put("type", "车扒预警");
            cbMap.put("today", 0);
            cbMap.put("total", 0);

            listMap.add(dklMap);
            listMap.add(zdrMap);
            listMap.add(ycpkMap);
            listMap.add(cbMap);

            resultModel.setTotalCount(null != listMap ? listMap.size() : 0);
            resultModel.setData(listMap);
            resultModel.setSuccess(true);
            resultModel.setMessage("查询成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultModel.setSuccess(false);
            resultModel.setMessage("查询失败！");
        } finally {
            return resultModel;
        }
    }

    /**
     * 常控预警公交列表接口
     */
    @RequestMapping("/getCkyjGjList")
    @ResponseBody
    public DataTableVo<Map<String, Object>> getCkyjGjList(HttpServletResponse response) {

        response.addHeader("Access-Control-Allow-Orrigin", "*");
        DataTableVo<Map<String, Object>> resultModel = new DataTableVo<Map<String, Object>>();

        try {

            List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
            Map<String, Object> params = new HashMap<String, Object>();
            //预警总数
            params.put("lx", "公交");
            Integer zdryTotalCount = bigdataService.getZdryyjCount(params);

            //今日预警数
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            params.put("startTime", sdf.format(new Date()));
            Integer zdryTodayCount = bigdataService.getZdryyjCount(params);

            //1大客流预警
            Map<String, Object> dklMap = new HashMap<String, Object>();
            dklMap.put("id", 0);
            dklMap.put("type", "大客流预警");
            dklMap.put("today", 0);
            dklMap.put("total", 0);

            //2重点人预警
            Map<String, Object> zdrMap = new HashMap<String, Object>();
            zdrMap.put("id", 1);
            zdrMap.put("type", "重点人预警");
            zdrMap.put("today", zdryTodayCount);
            zdrMap.put("total", zdryTotalCount);

            //3异常票卡预警
            Map<String, Object> ycpkMap = new HashMap<String, Object>();
            ycpkMap.put("id", 2);
            ycpkMap.put("type", "异常票卡预警");
            ycpkMap.put("today", 0);
            ycpkMap.put("total", 0);

            //4纵火闹事预警
            Map<String, Object> zhnsMap = new HashMap<String, Object>();
            zhnsMap.put("id", 3);
            zhnsMap.put("type", "纵火闹事预警");
            zhnsMap.put("today", 0);
            zhnsMap.put("total", 0);

            //5抢方向盘预警
            Map<String, Object> qfxpMap = new HashMap<String, Object>();
            qfxpMap.put("id", 4);
            qfxpMap.put("type", "抢方向盘预警");
            qfxpMap.put("today", 0);
            qfxpMap.put("total", 0);

            //6车扒预警
            Map<String, Object> cbMap = new HashMap<String, Object>();
            cbMap.put("id", 5);
            cbMap.put("type", "车扒预警");
            cbMap.put("today", 0);
            cbMap.put("total", 0);

            listMap.add(dklMap);
            listMap.add(zdrMap);
            listMap.add(ycpkMap);
            listMap.add(zhnsMap);
            listMap.add(qfxpMap);
            listMap.add(cbMap);

            resultModel.setTotalCount(null != listMap ? listMap.size() : 0);
            resultModel.setData(listMap);
            resultModel.setSuccess(true);
            resultModel.setMessage("查询成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultModel.setSuccess(false);
            resultModel.setMessage("查询失败！");
        } finally {
            return resultModel;
        }
    }

    /**
     * 常控预警公交列表接口
     */
    @RequestMapping("/getGjSxtList")
    @ResponseBody
    public DataTableVo<Map<String, Object>> getGjSxtList(@RequestParam(name = "cph") String cph, HttpServletResponse response) {

        response.addHeader("Access-Control-Allow-Orrigin", "*");
        DataTableVo<Map<String, Object>> resultModel = new DataTableVo<Map<String, Object>>();

        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("cph", cph);
            List<Map<String, Object>> gjSxtList = bigdataService.getGjSxtList(params);
            resultModel.setTotalCount(null != gjSxtList ? gjSxtList.size() : 0);
            resultModel.setData(gjSxtList);
            resultModel.setSuccess(true);
            resultModel.setMessage("查询成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultModel.setSuccess(false);
            resultModel.setMessage("查询失败！");
        } finally {
            return resultModel;
        }
    }


    @RequestMapping("/categoryStatistics")
    @ResponseBody
    public String home() {
        return "hello spring ";

    }


    /**
     * 公交地铁 分类统计
     */

    @RequestMapping("/queryBkryyjCategoryStatistics/{bjdd}")
    @ResponseBody
    public ResultSet queryBkryyjCategoryStatistics(@PathVariable("bjdd") String bjdd, HttpServletResponse response) {
        response.addHeader("Access-Control-Allow-Orrigin", "*");
        ResultSet resultSet=    new ResultSet();
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if ("gj".equalsIgnoreCase(bjdd)) {
                params.put("bjdd", "公交");
            }
            if ("dt".equalsIgnoreCase(bjdd)) {
                params.put("bjdd", "地铁");
            }
            resultSet.setData(bigdataService.queryBkryyjCategoryStatistics(params) );
            resultSet.setFlag(true);
            resultSet.setCode(200);
            resultSet.setMessage("查询成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultSet.setCode(400);
            resultSet.setFlag(false);
            resultSet.setMessage("查询失败！");
        } finally {
            return resultSet;
        }
    }


}
